import axios from "axios";
import { ElMessage } from 'element-plus'
import router from '@/router/index'
import config from '~/config'
import { localGet } from './index'

const ENV = import.meta.env;
const serve = axios.create({
  baseURL: config[ENV.MODE].baseURL,
  timeout: 30000,
  withCredentials: true,
  headers: {
    'token': localGet('token'),
    'Content-Type': 'application/json'
  }
})
serve.interceptors.response.use(res => {
  if (typeof res.data !== 'object') {
    ElMessage.warning('服务端异常！')
    return Promise.reject(res)
  }
  if (res.data.resultCode != 200) {
    if (res.data.message) ElMessage.warning(res.data.message)
    if (res.data.resultCode == 419) {
      router.push({ path: '/login' })
    }
    return Promise.reject(res.data)
  }

  return res.data.data
})

export default serve;